package cn.yunhe.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yunhe.dao.DrugDao;
import cn.yunhe.entity.Drug;
import cn.yunhe.entity.PageNo;
import cn.yunhe.service.DrugService;

@Service("drugService")
public class DrugServiceImpl implements DrugService {
	@Autowired
	private DrugDao drugDao;
	@Override
	public List<Drug> selectDrug() {
		return drugDao.selectDrug();
	}

	
	@Override
	public int updateDrugByDrid(int drid, int drnum) {
		
		return drugDao.updateDrugByDrid(drid, drnum);
	}
	@Override
	public List<Drug> selectDrug2(int drid) {
		
		return drugDao.selectDrug2(drid);
	}


	@Override
	public List<Drug> findDrugAll() {

		return drugDao.findDrugAll();
	}

	public DrugDao getDrugDao() {
		return drugDao;
	}

	public void setDrugDao(DrugDao drugDao) {
		this.drugDao = drugDao;
	}

	@Override
	public List<Drug> findDrygAllByLikeName(String name) {

		return drugDao.findDrygAllByLikeName(name);
	}

	@Override
	public int addDrug(Drug drug) {

		return drugDao.addDrug(drug);
	}

	@Override
	public int deleteDrugByDrstate(int drid) {

		return drugDao.deleteDrugByDrstate(drid);
	}

	@Override
	public int updateDrug(Drug drug) {

		return drugDao.updateDrug(drug);
	}

	@Override
	public Drug findDrugByDrid(int drid) {

		return drugDao.findDrugByDrid(drid);
	}

	@Override
	public List<Drug> findDrygPageNoByLikeName(String name, int pageNo, int pageSize) {
		// 分页 查询

		return drugDao.findDrygPageNoByLikeName(name, pageNo, pageSize);
	}

	@Override // 分页 封装的方法
	public PageNo findPageNo(PageNo pno) {
		System.out.println("================pageNo!!!!!=========="+",drname:::");
		System.out.println(pno.getPageNo()+","+pno.getIsSco()+","+pno.getStart()+","+pno.getEnd()+","+",,,"+pno.getDrname());
		// 总记录数
		if(pno.getDrname() == null && pno.getDrname() != ""){
			pno.setDrname("");
		}
		List<Drug> drlist2 = drugDao.findDrygAllByLikeName("%" + pno.getDrname() + "%"); // 总记录数   为 null 查不出来 必须为 字符串 "" ; 
		int totalPageNo = 0;
		for (Drug D : drlist2) {
			totalPageNo += 1;
		}
		int sum = 0;
		if (totalPageNo % 5 == 0) {
			sum = totalPageNo / 5;
		} else {
			sum = totalPageNo / 5 + 1;
		}
		System.out.println("totalPageNo:::"+totalPageNo);
		List<Integer> list = new ArrayList<Integer>();
		// 当前页码
		int pageNo = 1;
		// int totalPageNo = 80;
		int start = 1;
		int end = 1;
		String pageNoParam = pno.getPageNo();
		String isSco = pno.getIsSco();
		if (isSco == null) {
			if (pageNoParam != null) { // && pageNoParam.trim().equals("")
				pageNo = Integer.parseInt(pageNoParam);
			}
			if (sum < 5) {
				start = 1;
				end = sum;
			} else if (pageNo + 2 >= sum) {
				end = sum;
				start = end - 2;
			} else if (pageNo - 5 <= 1) {
				start = 1;
				end = start + 4;
			} else {
				start = pageNo - 2;
				end = pageNo + 2;
			}
			for (int i = start; i <= end; i++) {
				list.add(i);
			}
		} else if (isSco.equals("1")) {
			String strStr = pno.getStart();
			String endStr = pno.getEnd();
			if (endStr != null && !endStr.trim().equals("")) {
				end = Integer.parseInt(endStr);
			}
			if (strStr != null && !strStr.trim().equals("")) {
				start = Integer.parseInt(strStr);
			}
			if (end + 5 > sum) {
				end = sum;
				start = end - 4;
			} else {
				end += 5;
				start += 5;
			}
			pageNo = (start + end) / 2 +1;
			for (int i = start; i <= end; i++) {
				list.add(i);
			}
		} else {
			String strStr = pno.getStart();
			String endStr = pno.getEnd();
			if (endStr != null && !endStr.trim().equals("")) {
				end = Integer.parseInt(endStr);
			}
			if (strStr != null && !strStr.trim().equals("")) {
				start = Integer.parseInt(strStr);
			}
			if (start - 5 < 1) {
				start = 1;
				end = start + 4;
			} else {
				end -= 5;
				start -= 5;
			}
			pageNo = (start + end) / 2+1;
			for (int i = start; i <= end; i++) {
				list.add(i);
			}
		} // 查询记录
		int num1 = 0;
		int num2 = 10;
		if (pageNo == 1) {
			num1 = 0;
		} else {
			num1 = 0 + (pageNo - 1) * 5;
		}
		System.out.println("pageNo:::"+pageNo);
		System.out.println("num1::"+num1+",totalPageNo::"+totalPageNo+",start::"+start+",end::"+end);
		for (Integer i : list) {
			System.out.println("i:::"+i);
		}
		pno.setTotalPageNo(totalPageNo);
		pno.setEnd(String.valueOf(end));
		pno.setStart(String.valueOf(start));
		pno.setList(list);
		pno.setPageNo(String.valueOf(pageNo));
		pno.setPageNo2(num1);
		pno.setSum(sum);
		return pno;
	}

}
